﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;

namespace Autorent.Desktop.Modules.Orders.Services
{
    public class AutoOrdersRepository :AutorentEntitiesContext, IAutoOrdersRepository
    {
        public ObservableCollection<AutoOrder> GetOrders()
        {
            try
            {
                LoadRelatedEntities();
                return new ObservableCollection<AutoOrder>(Context.AutoOrders);
            }
            catch (Exception)
            {
                throw new AutorentModelException("Orders loading error occured!");
            }
        }

        public ObservableCollection<AutoOrder> GetOrdersByManager(decimal systemUserId)
        {
            try
            {
                LoadRelatedEntities();
                return new ObservableCollection<AutoOrder>(Context.AutoOrders.Where<AutoOrder>(a => a.AutoOrderRequestApproved.Equals(true) && a.Sys_SystemUser_ID.Equals(systemUserId)));
            }
            catch (Exception)
            {
                throw new AutorentModelException("Orders loading error  occured!");
            }
        }

        public ObservableCollection<AutoOrder> GetNewOrders()
        {
            try
            {
                LoadRelatedEntities();
                return new ObservableCollection<AutoOrder>(Context.AutoOrders.Where<AutoOrder>(a => a.AutoOrderRequestApproved.Equals(false)));
            }
            catch (Exception)
            {
                throw new AutorentModelException("Orders loading error  occured!");
            }
        }

        private void LoadRelatedEntities()
        {
            foreach (AutoOrder a in Context.AutoOrders)
            {
                Context.LoadProperty(a, "Client");
                Context.LoadProperty(a, "Office");
                Context.LoadProperty(a, "Office1");
                Context.LoadProperty(a, "Auto");
                Context.LoadProperty(a, "AutoClass");
                Context.LoadProperty(a.Auto, "AutoClass");
                Context.LoadProperty(a, "Payment");
            }
        }

        public void AddAutoOrder(AutoOrder autoOrder)
        {
            try
            {
                Context.AddToAutoOrders(autoOrder);
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Add auto order error occured!");
            }
        }

        public void UpdateAutoOrder(AutoOrder autoOrder)
        {
            try
            {
                Context.UpdateObject(autoOrder);
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Update auto order error occured!");
            }
        }

        public void DeleteAutoOrder(AutoOrder autoOrder)
        {
            try
            {
                Context.DeleteObject(autoOrder);
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Delte auto order failed!");
            }
        }
    }
}
